
class DeckMgr {
    constructor () {
        this.currentIndex = 0;
        this.deck = null;
    }

    setDeck(deck) {
        this.deck = deck;
        this.deck.initCards();
    }

    shuffle() {
        //数组重新排序
        var allCards = this.deck.getAllCards();
        for( let i = 0; i < allCards.length; ++i ) {
            let lastIndex = allCards.length - 1 - i;
            let index = Math.floor( Math.random() * lastIndex );
            let t = allCards[ index ];
            allCards[ index ] = allCards[ lastIndex ];
            allCards[ lastIndex ] = t;
        }
    }

    draw() {
        if(this.currentIndex == this.deck.allCards.length){
            return -1;
        }
        var card = this.deck.allCards[this.currentIndex];
        this.currentIndex++;
        return card;
    }

    getDeck() {
        var deck = [];
        for (let i = this.currentIndex; i < this.deck.allCards.length; ++i) {
            deck.push(this.deck.allCards[i]);
        }
        return deck;
    }
}

exports.DeckMgr = DeckMgr;